package pers.csw.lookBook.controller.front;

import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.csw.lookBook.common.Res;
import pers.csw.lookBook.dto.BookRackDto;
import pers.csw.lookBook.entity.BookRack;
import pers.csw.lookBook.service.BookRackService;

import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/book-rack")
public class BookRackController {
    @Autowired
    BookRackService bookRackService;

    /**
     * 通过用户id查询该用户书架
     * @param userId 用户id
     * @return 返回书架
     */
    @SaCheckLogin
    @GetMapping("/getBookRackByUserId/{userId}")
    public Res getBookRackByUserId(@PathVariable Integer userId){
        List<BookRackDto> bookRackByUserId = bookRackService.getBookRackByUserId(userId);
        return Res.success(bookRackByUserId);
    }



    /**
     * 通过id删除书架某本书的信息
     * @param id 书架id
     * @return 返回是否成功
     */
    @SaCheckLogin
    @DeleteMapping("/deleteBookRackById/{id}")
    public Res deleteBookRackById(@PathVariable Integer id){
        boolean b = bookRackService.removeById(id);
        return Res.success(b);
    }

    /**
     * 修改书架中某本书籍信息
     * @param bookRack 书架类
     * @return 返回是否成功
     */
    @SaCheckLogin
    @PutMapping("/updateBookRackByBookRack")
    public Res updateBookRackByBookRack(@RequestBody BookRack bookRack){
        bookRack.setUpdateTime(new Date());
        boolean b = bookRackService.updateById(bookRack);
        return Res.success(b);
    }

    /**
     * 添加书架
     * @param bookRack 书架对象
     * @return 返回是否成功
     */
    @SaCheckLogin
    @PostMapping("/insertBookRack")
    public Res insertBookRack(@RequestBody BookRack bookRack){
        if(bookRack.getChapterIndex()==null){
            bookRack.setChapterIndex(1);
        }
        bookRack.setCreateTime(new Date());
        boolean save = bookRackService.save(bookRack);
        return Res.success(save);
    }


    /**
     * 通过用户id和书籍id获取书籍是否在书架上
     * @param userId 用户id
     * @param bookId 书籍id
     * @return 返回查询的数据
     */
    @SaCheckLogin
    @GetMapping("/getBookRackByUserIdAndBookId/{userId}/{bookId}")
    public Res getBookRackByUserIdAndBookId(@PathVariable Integer userId, @PathVariable Integer bookId){
        BookRack bookRackByUserIdAndBookId = bookRackService.getBookRackByUserIdAndBookId(userId, bookId);
        return Res.success(bookRackByUserIdAndBookId);
    }

}
